package com.ds.dsm.view.config.form;

import com.ds.common.JDSException;
import com.ds.config.ResultModel;
import com.ds.dsm.view.config.action.CustomBuildAction;
import com.ds.dsm.view.config.service.FormConfigService;
import com.ds.esd.custom.annotation.CustomAnnotation;
import com.ds.esd.custom.annotation.FormViewAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.form.CustomFormViewBean;
import com.ds.esd.custom.form.enums.CustomFormMenu;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.nav.annotation.NavGroupAnnotation;
import com.ds.esd.custom.toolbar.BottomBarMenu;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import com.ds.esd.tool.ui.enums.Dock;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(path = "/dsm/view/config/form/")
@BottomBarMenu(menuClass = CustomBuildAction.class)
@NavGroupAnnotation(
        bottombarMenu = {CustomFormMenu.Save, CustomFormMenu.ReSet}, customService = FormConfigService.class)
public class FormInfoGroup {
    @CustomAnnotation(uid = true, hidden = true)
    private String esdclass;

    @CustomAnnotation(pid = true, hidden = true)
    private String viewInstId;
    @CustomAnnotation(pid = true, hidden = true)
    private String methodName;


    public FormInfoGroup() {
    }

    @RequestMapping(method = RequestMethod.POST, value = "FormBaseView")
    @FormViewAnnotation
    @ModuleAnnotation(imageClass = "spafont spa-icon-values", caption = "基础信息", dock = Dock.top, height = "200")
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ResultModel<FormBaseView> getFormBaseView(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ResultModel<FormBaseView> result = new ResultModel<FormBaseView>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            MethodConfig methodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
            CustomFormViewBean formViewBean = (CustomFormViewBean) methodAPIBean.getView();
            result.setData(new FormBaseView(formViewBean));
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return result;
    }


    @RequestMapping(method = RequestMethod.POST, value = "FormRowView")
    @FormViewAnnotation
    @ModuleAnnotation(imageClass = "spafont spa-icon-c-dialog", caption = "行属性配置", dock = Dock.fill)
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ResultModel<FormRowView> getFormRowView(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ResultModel<FormRowView> result = new ResultModel<FormRowView>();
        try {

            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            MethodConfig methodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
            CustomFormViewBean formViewBean = (CustomFormViewBean) methodAPIBean.getView();
            result.setData(new FormRowView(formViewBean));
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return result;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public String getEsdclass() {
        return esdclass;
    }

    public void setEsdclass(String esdclass) {
        this.esdclass = esdclass;
    }

    public String getViewInstId() {
        return viewInstId;
    }

    public void setViewInstId(String viewInstId) {
        this.viewInstId = viewInstId;
    }
}
